package com.fan.shop.businesslogic;

import org.apache.log4j.Logger;

import com.fan.shop.domain.TransactionReference;
import com.fan.shop.global.ShopGlobalUtils;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.TransactionReferenceService;

public class NewNumberGenerationProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(NewNumberGenerationProcess.class);

	private TransactionReferenceService transactionReferenceService;
	private String transactionCode;
	private String errorMessage;
	private String newNumber;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		NewNumberGenerationProcess.logger = logger;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public String getTransactionCode() {
		return transactionCode;
	}

	public void setTransactionCode(String transactionCode) {
		this.transactionCode = transactionCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getNewNumber() {
		return newNumber;
	}

	public void setNewNumber(String newNumber) {
		this.newNumber = newNumber;
	}

	private String generateNumber(String prefix, int last, int max) {
		String result = "";

		String maxStr = String.valueOf(max);
		int maxLength = maxStr.length();

		String lastStr = String.valueOf(last);
		int lastStrLength = lastStr.length();

		int gap = maxLength - lastStrLength;
		for (int i = 0; i < gap; i++) {
			lastStr = "0" + lastStr;
		}

		result = prefix + lastStr;
		return result;
	}

	@Override
	public Integer processImpl() {
		
		if(!transactionReferenceService.hasTransactionReference(transactionCode))
		{
			errorMessage = ShopGlobalUtils.getMessage("transaction_reference_no_exist");
			
			return ShopGlobalVariables.FAILED;
		}
		else
		{
			TransactionReference transactionReference = transactionReferenceService.getTransactionReference(transactionCode);
			String prefix = transactionReference.getPrefix();
			int start = transactionReference.getStart();
			int last = transactionReference.getLast();
			int max = transactionReference.getMax();
			
			
			if(last >= max || start >= max)
			{
				errorMessage = ShopGlobalUtils.getMessage("transaction_reference_reach_max");
				return ShopGlobalVariables.FAILED;
			}
			
			if(last < start)
			{
				last = start;
			}
			
			newNumber = generateNumber(prefix, last, max);
			
			transactionReference.setLast(last+1);
			transactionReferenceService.updateTransactionReference(transactionReference);
			return ShopGlobalVariables.SUCCESS;
		}
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		NewNumberGenerationProcess test = new NewNumberGenerationProcess();
		String result = test.generateNumber("ITEM-", 89, 99999999);
		System.out.println(result);
	}

}
